﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class cms_ListGalleryImage : System.Web.UI.Page
{
    string path = "~/Images/Gallery_Image/";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Response.Redirect("Default.aspx");
        }
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Id"]))
            {
                if (Request.QueryString.Count >= 2)
                {
                    lbGalleryName.Text = Convert.ToString(Request.QueryString["Name"]);
                }
                DBClass db = new DBClass();
                DataSet ds = db.GetListGalleryImageByGalleryId(Convert.ToInt32(Request.QueryString["Id"]));
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    GVGalleryImage.DataSource = ds.Tables[0];
                    GVGalleryImage.DataBind();
                }
            }
            else
            {
                Response.Redirect("ListGallery.aspx");
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (FileImage.HasFile)
        {
            DBClass db = new DBClass();
            string image = "";
            int i = db.InsertGalleryImage(image, txtNote.Text.Trim(), Convert.ToInt32(Request.QueryString["Id"]));
            if (i > 0 && FileImage.HasFile)
            {
                image = i + System.IO.Path.GetExtension(FileImage.FileName);
                FileImage.SaveAs(Server.MapPath(path + image));
                db.UpdateGalleryImage(i, image, txtNote.Text, Convert.ToInt32(Request.QueryString["Id"]));
            }
            Response.Redirect("ListGalleryImage.aspx?Id=" + Convert.ToInt32(Request.QueryString["Id"]) + "&Name=" + Convert.ToString(Request.QueryString["Name"]));
        }
        else
        {
            FileImage.BorderColor = System.Drawing.Color.Red;
            FileImage.BorderWidth = new Unit(1);
        }
    }
    protected void GVGalleryImage_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        DBClass db = new DBClass();
        switch (e.CommandName)
        {
            case "Edit":
                
                break;
            case "Delete":
                string image = "";
                DataSet ds = db.GetListGalleryImageById(Convert.ToInt32(e.CommandArgument));
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    image = ds.Tables[0].Rows[0]["Image"].ToString();
                }
                if(System.IO.File.Exists(Server.MapPath(path+image)))
                {
                    System.IO.File.Delete(Server.MapPath(path + image));
                }
                db.DeleteGalleryImage(Convert.ToInt32(e.CommandArgument));
                break;
            default:
                break;
        }
        Response.Redirect("ListGalleryImage.aspx?Id=" + Convert.ToInt32(Request.QueryString["Id"]) + "&Name=" + Convert.ToString(Request.QueryString["Name"]));
    }
}